package com.itcast.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.mapper.PermissionMapper;
import com.itcast.mapper.RoleMapper;
import com.itcast.mapper.UserMapper;
import com.itcast.pojo.Permission;
import com.itcast.pojo.Role;
import com.itcast.pojo.Setmeal;
import com.itcast.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = Userservice.class)
@Transactional
public class UserServiceImpl implements  Userservice{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 获取登录对象
     * @param username
     * @return
     */
    @Override
    public User findByName(String username) {
        User user = userMapper.findByName(username);
        if (user == null){
            return null;
        }
        Integer id = user.getId();
        Set<Role> roles = roleMapper.findById(id);
        if(roles != null && roles.size() > 0){
            for(Role role:roles){
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionMapper.finById(roleId);
                if(permissions != null && permissions.size() >0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;
    }
}
